package com.ita.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.jpa.criteria.path.SetAttributeJoin;
//import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ita.annotation.ControllerLog;
import com.ita.dto.OrderDto;
import com.ita.pojo.Food;
import com.ita.pojo.Order;
import com.ita.pojo.OrderItem;
import com.ita.service.OrderService;
import com.ita.util.Constant;
import com.ita.vo.JsonResult;
import com.ita.vo.OrderItemVo;
import com.ita.vo.OrderVo;

@RestController
@RequestMapping("/sec/order")
public class OrderController {

	@Resource(name = "orderService")
	private OrderService oService;


	@RequestMapping(value = "/list/{mid}", method = RequestMethod.PATCH)
	@ControllerLog(description="get order")
	public JsonResult get(@PathVariable("mid") String mid) {
		JsonResult result = new JsonResult();
		List<Order> orders = null;
		List<OrderVo> vo = new ArrayList<>();
		
		try{
			orders = oService.getOrder(mid);
			for(Order order:orders){
				vo.add(po2vo(order));
			}
			result.setCode(Constant.SUCCESS);
			result.setMsg("Success");
			result.setData(vo);
			
		}catch (Exception e) {
			result.setCode(Constant.FAIL);
			result.setMsg("Service Exception");
			return result;
		}
		
		
		return result;
	}
	
	@RequestMapping(value = "/get/{mid}/state/{state}", method = RequestMethod.PATCH)
	@ControllerLog(description="get order with state")
	public JsonResult getWithState(@PathVariable("mid") String mid,@PathVariable("state") int state) {
		
		
		JsonResult result = new JsonResult();
		List<Order> orders = null;
		List<OrderVo> vo = new ArrayList<>();
		
		try{
			orders = oService.getOrder(mid, state);
			for(Order order:orders){
				vo.add(po2vo(order));
			}
			result.setCode(Constant.SUCCESS);
			result.setMsg("Success");
			result.setData(vo);
		}catch (Exception e) {
			e.printStackTrace();
			result.setCode(Constant.FAIL);
			result.setMsg("Service Exception");
			return result;
		}
		
		return result;
	}
	
//	@RequestMapping(value = "/{oid}/affirm/{state}", method = RequestMethod.PATCH)
//	public JsonResult affirm(@PathVariable("oid") String oid, @PathVariable("state") int affirm) {
//		JsonResult result = new JsonResult();
//		int update = 0;
//
//		try {
//			update = oService.updateState(oid, affirm);
//		} catch (Exception e) {
//			result.setCode(Constant.FAIL);
//			result.setMsg("Service Exception");
//			return result;
//		}
//
//		if (update == 1) {
//			result.setCode(Constant.SUCCESS);
//		} else {
//			result.setCode(Constant.FAIL);
//		}
//
//		return result;
//	}
	
	
	private OrderVo po2vo(Order order) throws Exception{
		
		OrderVo vo =  new OrderVo();
		vo.setCreate_time(new Date(order.getCreate_time()));
		vo.setId(order.getId());
		vo.setC_address(order.getC_address());
		for(OrderItem orderItem:order.getOrderItemSet()){
			OrderItemVo tmp  = new OrderItemVo();
			tmp.setCount(orderItem.getCount());
			tmp.setFood(orderItem.getFood().getName());
			tmp.setPrice(orderItem.getFood().getPrice());
			tmp.setImage(orderItem.getFood().getImg());
			tmp.setId(orderItem.getFood().getId());
			vo.getOrderItem().add(tmp);
		}
		vo.setTotal(order.getTotal());
		vo.setState(order.getState());
		vo.setUsername(order.getcUser().getName());
		return vo;
		
	}
	
}
